package GoodsPackage;

import UsersPackage.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteGood.do")
public class DeleteGoodServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        try {
            String goods_id = request.getParameter("goods_id");

            GoodsDao goodsDao = new GoodsDao();

            // Assuming you have a method in UserDao for deleting a user by username
            if (goodsDao.deleteGoodByGoodid(goods_id) > 0) {
                response.getWriter().print("删除商品成功，在3秒后跳转回登录页面!");
                response.setHeader("Refresh", "3;url=menu/device/3-3.jsp"); // Redirect to login page after 3 seconds
            } else {
                response.getWriter().print("删除商品失败，在3秒后跳转回登录页面");
                response.setHeader("Refresh", "3;url=menu/device/3-3.jsp"); // Redirect to login page after 3 seconds
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Handle GET requests if needed
    }
}
